import Vue from 'vue'
import Vuex from 'vuex'
import {
  setUserToken,
  getUserToken,
  removeUserToken,
  setUserData,
  getUserData,
  removeUserData,
} from '@/utils/storage'
// import { newToken } from '@/api/user'
import axios from 'axios'
import store from '..'

Vue.use(Vuex)

export default {
  namespaced: true,
  state: {
    user: getUserToken() || null,
    userInfoData: getUserData() || null,
  },
  getters: {},
  mutations: {
    setUser(state, data) {
      setUserToken(data)
      state.user = data
    },
    setUserInfoData(state, data) {
      setUserData(data)
      store.userInfoData = data
    },
  },
  actions: {
    setUserInfo(store, data) {
      store.commit('setUser', data)
    },
    // async setNewToken(store, data) {
    //   const res = await axios({
    //     method: 'PUT',
    //     url: 'http://ttapi.research.itcast.cn/v1_0/authorizations',
    //     headers: {
    //       Authorization: `Bearer ${data}`,
    //       'Content-Type': 'application/x-www-form-urlencoded	',
    //     },
    //   })
    //   store.commit('setUser', {
    //     token: res.data.data.token, // 最新获取的可用 token
    //     refresh_token: data, // 还是原来的 refresh_token
    //   })
    // },
  },
}
